<?php
class UrlRewrite
{
    public static function rewrite()
    {
        if ($GLOBALS['_SERVER']['REQUEST_URI'] != "/index.php") {
            if (strpos($GLOBALS['_SERVER']['REQUEST_URI'], "index.php/")) {
                $GLOBALS['_SERVER']['REQUEST_URI'] = str_replace("index.php/", "", $GLOBALS['_SERVER']['REQUEST_URI']);
            }
        }
        else
        {
            $GLOBALS['_SERVER']['REQUEST_URI'] = str_replace("/index.php", "/Index",$GLOBALS['_SERVER']['REQUEST_URI']);
        }
        if ($GLOBALS['_SERVER']['REQUEST_URI'] != null) {
            $request = $GLOBALS['_SERVER']['REQUEST_URI'];
            $request_array = explode("/", $request);
            $lastValue = $request_array[count($request_array) - 1];
            if (sizeof($request_array) == 2) {
                $_GET['controller'] = $request_array[1];
                $_GET['method'] = "showIndex";
            }
            if (sizeof($request_array) == 3) {
                $_GET['controller'] = $request_array[1];
                $_GET['method'] = UrlRewrite::analysisMethod($request_array[2]);
            }
            if (sizeof($request_array) > 3) {
                $_GET['controller'] = $request_array[1];
                $_GET['method'] = $request_array[2];
                UrlRewrite::mappingQueryString($request_array[3]);
            }
            if (is_numeric($lastValue)) {
                //$_GET['method'] = "loadById";
                $_GET['id'] = $lastValue;
            }


        }
    }

    private static function  analysisMethod($methodName)
    {
        //如果是/news?ssss=dd的形式
        if (strpos($methodName, "?") > -1) {
            $parameters = explode("?", $methodName);
            UrlRewrite::analysisMethod($parameters[1]);
            return $parameters[0];
        }
        else
        {
            return $methodName;
        }
    }

    private static function mappingQueryString($parameters)
    {
        $parameter_array = explode("&", $parameters);
        foreach ($parameter_array as $key => $value) {
            $_GET[$key] = $value;
        }
    }
}

?>